package com.cdivtc.management.controller;

import com.cdivtc.management.aspect.Log;
import com.cdivtc.management.pojo.AchPatent;
import com.cdivtc.management.pojo.AchProject;
import com.cdivtc.management.pojo.AchThesis;
import com.cdivtc.management.pojo.Result;
import com.cdivtc.management.pojo.vo.AchPatentvo;
import com.cdivtc.management.pojo.vo.AchProjectvo;
import com.cdivtc.management.pojo.vo.AchThesisvo;
import com.cdivtc.management.service.AchievementServer;
import com.cdivtc.management.utils.IdWorker;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("api/achievement")
public class AchievementController {
    //    成果管理
    @Autowired
    private AchievementServer achievementServer;
    @Autowired
    private IdWorker idWorker;

    //    查询所有成果类型
    @RequestMapping("queryalltype")
    public Result queryAllType() {
        Map map = new HashMap<>();
        map.put("type", achievementServer.queryAllType());
        map.put("grades", achievementServer.queryAllgrades());

        return new Result().ok().addData(map);
    }    //    查询所有成果类型

    @RequestMapping("querybytypeid/{parentid}")
    public Result queryByTypeid(@PathVariable("parentid") String parentid) {
        System.out.println("parentid = " + parentid);
        return new Result().ok().addData(achievementServer.queryByIstype(parentid));
    }

    //添加论文分类、
    @RequestMapping("/addAchThesis")
    @Log("添加论文")
    public Result addAchThesis(@RequestBody AchThesisvo thesisvo) {
        AchThesis achThesis = new AchThesis();
        BeanUtils.copyProperties(thesisvo, achThesis);
        String id = idWorker.nextId() + "";
        achThesis.setId(id);
        achievementServer.addAchThesis(achThesis);
        achievementServer.updateFiles(thesisvo.getUploadFileList(), id);
        return new Result().ok().addData(achThesis);
    }

    //    添加项目
    @RequestMapping("/addAchProject")
    @Log("添加项目")
    public Result addAchProject(@RequestBody AchProjectvo projectvo) {
        AchProject project = new AchProject();
        BeanUtils.copyProperties(projectvo, project);
        String id = idWorker.nextId() + "";
        project.setId(id);
        System.out.println("project.getUploadFileList = " + projectvo.getUploadFileList());
        achievementServer.updateFiles(projectvo.getUploadFileList(), id);
        achievementServer.addAchProject(project);
        return new Result().ok().addData(project);

    }
    //    添加专利
    @RequestMapping("/addAchPatent")
    @Log("添加专利")
    public Result addAchPatent(@RequestBody AchPatentvo patentvo) {
        AchPatent achPatent = new AchPatent();
        BeanUtils.copyProperties(patentvo, achPatent);
        String id = idWorker.nextId() + "";
        achPatent.setId(id);
        achievementServer.addAchPatent(achPatent);
        achievementServer.updateFiles(patentvo.getUploadFileList(), id);
        return new Result().ok().addData(achPatent);
    }

    @RequestMapping("/addAchOther")
    @Log("添加其他成果")
    public Result addAchOther(@RequestBody AchThesis patent) {
        System.out.println("thesis = " + patent);
        achievementServer.addAchThesis(patent);
        return new Result().ok().addData(patent);
    }

    //根据某个用户id查询所有的成果
    @RequestMapping("queryByAchAllAndUid/{uid}")
    public Result queryByAchAllAndUid(@PathVariable("uid") String uid) {
        Map map = achievementServer.queryByAchAllAndUid(uid);
        return new Result().ok().addData(map);
    }
    @Log("修改成果")
    @RequestMapping(value = "updateAchThesis", method = RequestMethod.PUT)
    public Result updateAchThesis(@RequestBody AchThesisvo achThesisvo) {
        AchThesis achThesis = new AchThesis();
        BeanUtils.copyProperties(achThesisvo, achThesis);
//        修改文件的任务成果id
        achievementServer.updateFiles(achThesisvo.getUploadFileList(), achThesis.getId());
        achievementServer.updateAchThesis(achThesis);
        return new Result().ok().addmsg("修改成功");
    }
    @Log("修改成果")
    @RequestMapping(value = "updateAchPaject", method = RequestMethod.PUT)
    public Result updateAchPaject(@RequestBody AchProjectvo projectvo) {
        AchProject project = new AchProject();
        BeanUtils.copyProperties(projectvo, project);
        achievementServer.updateFiles(projectvo.getUploadFileList(), project.getId());
        achievementServer.updateAchPaject(project);
        return new Result().ok().addmsg("修改成功");
    }
    @Log("修改成果")
    @RequestMapping(value = "updateAchPatent", method = RequestMethod.PUT)
    public Result updateAchPatent(@RequestBody AchPatentvo achPatentvo) {
        AchPatent achPatent = new AchPatent();
        BeanUtils.copyProperties(achPatentvo,achPatent);
        achievementServer.updateFiles(achPatentvo.getUploadFileList(),achPatent.getId());
        achievementServer.updateAchPatent(achPatent);
        return new Result().ok().addmsg("修改成功");
    }



    //    根据成果id查询成果信息
    @RequestMapping(value = "queryAchById/{achid}", method = RequestMethod.GET)
    public Result queryAchById(@PathVariable("achid") String achid) {
        Map map = achievementServer.queryAchById(achid);
        return new Result().ok().addData(map);
    }

    //根据学院查询成果
    @RequestMapping(value = "/queryAchByCollage/{collage}", method = RequestMethod.GET)
    public Result queryAchByCollage(@PathVariable("collage") String collage) {
//根据成果等级查看103
//        查看位审核
        List map = achievementServer.queryAchByCollage(collage);
        return new Result().ok().addData(map);
    }

    //根据学院查询成果审核通过
    @RequestMapping(value = "/queryAchByCollagepass/{collage}", method = RequestMethod.GET)
    public Result queryAchByCollagepass(@PathVariable("collage") String collage) {
//根据成果等级查看103
        List map = achievementServer.queryAchByCollagepass(collage);
        System.out.println("map*********** = " + map);
        return new Result().ok().addData(map);
    }

    //    @RequestParam(value = "page", defaultValue = "0")
    @GetMapping("queryAchByclassAndTeacher/{teacherid}")
    public Result queryAchByclassAndTeacher(@PathVariable("teacherid") String teacherid) {
//          辅导员查询学生申请
//        查询辅导员所管理的班级
        List map = achievementServer.queryAchByclassAndTeacher(teacherid);
        return new Result().ok().addData(map);

    }

    @GetMapping("queryAchByclassAndTeacherpass/{teacherid}")
    public Result queryAchByclassAndTeacherpass(@PathVariable("teacherid") String teacherid) {
//          辅导员查询学生审核通过
//        查询辅导员所管理的班级
        List map = achievementServer.queryAchByclassAndTeacherpass(teacherid, "班级","0");
        return new Result().ok().addData(map);

    }



    //根据学院查询成果
    @RequestMapping(value = "/queryAchBySchool", method = RequestMethod.GET)
    public Result queryAchBySchool() {
//根据成果等级查看103
        List map = achievementServer.queryAchBySchool();
        return new Result().ok().addData(map);
    }

    //根据学院查询成果
    @RequestMapping(value = "/queryAchBySchoolpass", method = RequestMethod.GET)
    public Result queryAchBySchoolpass() {
//根据成果等级查看103
        List map = achievementServer.queryAchBySchoolpass();
        return new Result().ok().addData(map);
    }


    //    根据id修改状态
    @PutMapping("/updateAchState/{state}/{achid}/{grade}")
    @Log("审核成果")
    public Result updateAchState(@PathVariable("achid") String achid, @PathVariable("state") String state, String comment, @PathVariable("grade") String grade) {
//老师
//        grade  判断成果类型  班级 院级 校级
        if ("101".equals(grade)) {
//            班级   辅导员审核  通过 不通过
            achievementServer.uppdataAchstate(achid, state, comment);

        }

        achievementServer.uppdataAchstate(achid, state, comment);

        return new Result().ok();
    }

    //用户查询自己的成果。
    //根据用户名和输入的标题搜索
    @GetMapping("queryByAchwhereuid/{title}/{uid}")
    @ResponseBody
    public Result queryByAchwhereuid(@PathVariable("title") String title, @PathVariable("uid") String uid) {


        System.out.println("title = " + title);
        System.out.println("uid = " + uid);
        Map map = achievementServer.queryByAchwhereuid(title, uid);

        return new Result().ok().addData(map);
    }



}
